/* ===========================================================
 * NetworkEvolutionPlugin : Cytoscape plugin for visualizing stages of
 * protein networks evolution.
 * ===========================================================
 *
 *
 * Project Info:  http://bioputer.mimuw.edu.pl/modevo/
 * Sources: http://code.google.com/p/misiek/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * NetworkEvolutionPlugin  Copyright (C) 2008-2010
 * Authors:  Michal Wozniak (code) (m.wozniak@mimuw.edu.pl)
 *           Janusz Dutkowski (idea, data) (j.dutkowski@mimuw.edu.pl)
 *           Jerzy Tiuryn (supervisor) (tiuryn@mimuw.edu.pl)
 */

package ppine.ui.dataloading;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.border.TitledBorder;
import ppine.main.LoadedDataHandle;
import ppine.main.PluginDataHandle;

public class SpeciesInteractionsLoaderPanel extends javax.swing.JPanel {

    private String name;
    private String filepath;
    private InteractionsLoaderPanel intPanel;

    /** Creates new form SpeciesInteractionsLoaderPanel
     * @param name
     * @return 
     */
    public boolean checked() {
        return checkbox.isSelected();
    }

    public String tryGetFilepath() {
        return filepath;
    }

    public SpeciesInteractionsLoaderPanel(final String name, final InteractionsLoaderPanel intPanel) {
        initComponents();
        this.name = name;
        this.intPanel = intPanel;
        TitledBorder border = (TitledBorder) this.getBorder();
        border.setTitle(name);
        setActualLoadedUIState();
    }

    public Double tryGetTreshold() {
        if (checkbox.isSelected()) {
            try {
                Double treshold = Double.valueOf(tresholdField.getText());
                return treshold;
            } catch (NumberFormatException ex) {
                return null;
            }
        } else {
            return null;
        }
    }

    public String getSpeciesName() {
        return name;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        checkbox = new javax.swing.JCheckBox();
        tresholdField = new javax.swing.JTextField();
        openButton = new javax.swing.JButton();
        filepathLabel = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder("species name"));

        checkbox.setName("checkbox"); // NOI18N
        checkbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkboxActionPerformed(evt);
            }
        });

        tresholdField.setEnabled(false);
        tresholdField.setName("tresholdField"); // NOI18N

        openButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ppine/resources/icons/com.png"))); // NOI18N
        openButton.setText("Open");
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("ppine/ui/resources/Loading"); // NOI18N
        openButton.setToolTipText(bundle.getString("OpenInteractionsForSpeciesButton.ToolTip")); // NOI18N
        openButton.setEnabled(false);
        openButton.setName("openButton"); // NOI18N
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });

        filepathLabel.setText("filepath");
        filepathLabel.setName("filepathLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(checkbox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tresholdField, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(openButton, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(filepathLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tresholdField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(openButton)
                        .addComponent(filepathLabel))
                    .addComponent(checkbox))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void checkboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkboxActionPerformed
        if (this.checkbox.isSelected()) {
            tresholdField.setEnabled(true);
            openButton.setEnabled(true);
            filepathLabel.setEnabled(true);
        //   filepath = PluginDataHandle.getLoadingDataHandle().getSpeciesFilename(name);
        //   if (filepath != null) {
        //        filepathLabel.setText(filepath);
        //    }
        } else {
            tresholdField.setEnabled(false);
            openButton.setEnabled(false);
            filepathLabel.setEnabled(false);
        /*if (filepath != null) {
        PluginDataHandle.getLoadingDataHandle().deleteInteractionFilename(name, filepath);
        }*/
        }
    }//GEN-LAST:event_checkboxActionPerformed

    private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(fc);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            filepath = file.getAbsolutePath();
            intPanel.enableLoadButton();
            //      PluginDataHandle.getLoadingDataHandle().addInteractionFilename(name, filepath);
            filepathLabel.setText(filepath);
        }
    }//GEN-LAST:event_openButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox checkbox;
    private javax.swing.JLabel filepathLabel;
    private javax.swing.JButton openButton;
    private javax.swing.JTextField tresholdField;
    // End of variables declaration//GEN-END:variables

    public void setActualLoadedUIState() {
        LoadedDataHandle ldh = PluginDataHandle.getLoadedDataHandle();
        String filename = ldh.getSpeciesInteractionsFilename(name);
        Double treshold = ldh.getSpeciesInteractionsTreshold(name);
        if (filename == null) {
            checkbox.setSelected(false);
            tresholdField.setEnabled(false);
            openButton.setEnabled(false);
            filepathLabel.setEnabled(false);
            tresholdField.setText("0.7");
        } else {
            checkbox.setSelected(true);
            tresholdField.setEnabled(true);
            openButton.setEnabled(true);
            filepath = filename;
            filepathLabel.setEnabled(true);
            filepathLabel.setText(filename);
            if (treshold != null) {
                tresholdField.setText(String.valueOf(treshold));
            } else {
                tresholdField.setText("0.7");
            }
        }
    }
}
